//
// Created by 32827 on 2024/2/15.
//
#include "widget/yzl_ui_widget_pp_text.hpp"
#include "yzl.h"

WidgetText::WidgetText(YZL_WIDGET *Ram, int x, int y, int z, int width, int height, utf8 *text) : WidgetBase(Ram, x, y, z, width, height) {
    text_color = WHITE;

    yzl_ui_font_data_init(&fontPixel,&this->text,&textLen,text);

    uint32_t size= yzl_ui_get_string_width(fontPixel,this->text, textLen);  //调整控件大小
    uint16_t nWidth=*((uint16_t *)(&size)+1);
    uint16_t nHeight=*(uint16_t *)(&size);
    if(width-2<nWidth){
        instance->width=nWidth+2;
    }
    if(height-2<nHeight){
        instance->height=nHeight+2;
    }
}

WidgetText::WidgetText(YZL_WIDGET *Ram, UI_Rect *rect, int z, utf8 *text) : WidgetText(Ram, rect->x, rect->y, z, rect->width, rect->height,text) {

}

void WidgetText::drawP() {
    WidgetBase::drawP();
    if(instance->status==YZL_UI_CTR_EVENT_TOUCH_UP){
        uint32_t size= yzl_ui_get_string_width(fontPixel,text, textLen);
        uint16_t width=(size>>16);
        uint16_t height=(size&0xffff);
        yzl_ui_draw_string(instance->x+(instance->width-width)/2,instance->y+(instance->height-height)/2,instance->x+instance->width,
                           text,textLen,WHITE,text_color);
    }else if(instance->status==YZL_UI_CTR_EVENT_TOUCH_DOWN){
        uint32_t size= yzl_ui_get_string_width(fontPixel,text, textLen);
        uint16_t width=(size>>16);
        uint16_t height=(size&0xffff);
        yzl_ui_draw_string(instance->x+(instance->width-width)/2,instance->y+(instance->height-height)/2,instance->x+instance->width,
                           text,textLen,WHITE,text_color);
    }
    instance->isRender=YZF_FALSE;
}

void WidgetText::disposeP() {
    YZL_MEM_MANAGER.pFree(text);
    YZL_MEM_MANAGER.pFree(fontPixel);
    WidgetBase::disposeP();
}

void WidgetText::setText(utf8 *srcText) {
    BackUp();
    yzl_ui_font_data_init(&fontPixel, &this->text, &textLen, srcText);
    uint32_t size= yzl_ui_get_string_width(fontPixel,this->text, textLen);  //调整控件大小
    uint16_t nWidth=*((uint16_t *)(&size)+1);
    uint16_t nHeight=*(uint16_t *)(&size);
    if(instance->width-2<nWidth){
        instance->width=nWidth+2;
    }
    if(instance->height-2<nHeight){
        instance->height=nHeight+2;
    }
    instance->isRender=YZF_TRUE;
}
